package dao.impl;

import dao.WareHouseProductDao;
import entity.Warehouse_Product;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import util.C3P0Util;

import java.sql.SQLException;

public class WareHouseProductDaoImpl implements WareHouseProductDao{
	//设计方法用于返回操作数据库的qr对象
	private QueryRunner getQueryRunner(){
		return new QueryRunner(C3P0Util.getDataSource());
	}

	@Override
	public Warehouse_Product queryWarehouse_ProductById(Integer wp_id) {
		QueryRunner qr = this.getQueryRunner();
		String sql = "select * from warehouse_product where wp_id = ?";
		Warehouse_Product wp = null;
		try {
			wp = qr.query(sql, wp_id,new BeanHandler<Warehouse_Product>(Warehouse_Product.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return wp;
	}

	@Override
	public int updateWarehouse_ProductByIdAndCount(Integer wp_id, Integer count) {
		QueryRunner qr = this.getQueryRunner();
		String sql = "update warehouse_product set current_cnt=current_cnt+? WHERE wp_id=?";
		Object[] params = {count,wp_id};
		int i = 0;
		try {
			i = qr.update(sql,params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}

	@Override
	public Integer queryWarehouseProductIdByProductIdAndModelId(Integer productId, Integer ModelId) {
		QueryRunner qr = this.getQueryRunner();
		String sql = "select * from warehouse_product where product_id=? and model_id = ?";
		Warehouse_Product wp = null;
		Object[] params = {productId,ModelId};
		try {
			wp = qr.query(sql, params,new BeanHandler<Warehouse_Product>(Warehouse_Product.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return wp.getWp_id();
	}

}
